Trò chơi Tic-Tac-Toe, game đánh caro full source code
- GUICustomAuth.cs
- DemoFriendsAndCustomAuth /
- Demos /
- Photon Unity Networking /
- Assets /
- project /
2 using System.Collections;
3
4 public class GUICustomAuth : MonoBehaviour
5 {
6 public Rect GuiRect;
7
8 private string authName = "usr";
9 private string authToken = "usr";
10 private string authDebugMessage = string.Empty;
11
12
13 void Start()
14 {
15 GuiRect = new Rect(Screen.width / 4, 80, Screen.width / 2, Screen.height - 100);
16 }
17
18
19 public void OnJoinedLobby()
20 {
21 // for ease of use, this script simply deactivates itself on successful connect
22 this.enabled = false;
23 }
24
25
26 /// <summary>
27 /// This method is called when Custom Authentication is setup for your app but fails for any reasons.
28 /// </summary>
29 /// <remarks>
30 /// Unless you setup a custom authentication service for your app (in the Dashboard), this won't be called.
31 /// If authentication is successful, this method is not called but OnJoinedLobby, OnConnectedToMaster and the
32 /// others will be called.
33 /// </remarks>
34 /// <param name="debugMessage"></param>
35 public void OnCustomAuthenticationFailed(string debugMessage)
36 {
37 this.authDebugMessage = debugMessage;
38 SetStateAuthFailed();
39 }
40
41
42 enum GuiState { AuthOrNot, AuthInput, AuthHelp, AuthFailed }
43 private GuiState guiState;
44 public GameObject RootOf3dButtons;
45
46 public void SetStateAuthInput()
47 {
48 RootOf3dButtons.SetActive(false);
49 guiState = GuiState.AuthInput;
50 }
51
52 public void SetStateAuthHelp()
53 {
54 RootOf3dButtons.SetActive(false);
55 guiState = GuiState.AuthHelp;
56 }
57
58 public void SetStateAuthOrNot()
59 {
60 RootOf3dButtons.SetActive(true);
61 guiState = GuiState.AuthOrNot;
62 }
63
64 public void SetStateAuthFailed()
65 {
66 RootOf3dButtons.SetActive(false);
67 guiState = GuiState.AuthFailed;
68 }
69
70 public void ConnectWithNickname()
71 {
72 RootOf3dButtons.SetActive(false);
73
74 PhotonNetwork.AuthValues = null; // null by default but maybe set in a previous session.
75 PhotonNetwork.ConnectUsingSettings("1.0");
76
77 // PhotonNetwork.playerName gets set in GUIFriendFinding
78 // a UserID is not used in this case (no AuthValues set)
79 }
80
81 void OnGUI()
82 {
83 if (PhotonNetwork.connected)
84 {
85 GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
86 return;
87 }
88
89
90 GUILayout.BeginArea(GuiRect);
91 switch (guiState)
92 {
93 case GuiState.AuthFailed:
94 GUILayout.Label("Authentication Failed");
95
96 GUILayout.Space(10);
97
98 GUILayout.Label("Error message:\n'" + this.authDebugMessage + "'");
99
100 GUILayout.Space(10);
101
102 GUILayout.Label("For this demo set the Authentication URL in the Dashboard to:\nhttp://photon.webscript.io/auth-demo-equals");
103 GUILayout.Label("That authentication-service has no user-database. It confirms any user if 'name equals password'.");
104 GUILayout.Label("The error message comes from that service and can be customized.");
105
106 GUILayout.Space(10);
107
108 GUILayout.BeginHorizontal();
109 if (GUILayout.Button("Back"))
110 {
111 SetStateAuthInput();
112 }
113 if (GUILayout.Button("Help"))
114 {
115 SetStateAuthHelp();
116 }
117 GUILayout.EndHorizontal();
118 break;
119
120 case GuiState.AuthHelp:
121
122 GUILayout.Label("By default, any player can connect to Photon.\n'Custom Authentication' can be enabled to reject players without valid user-account.");
123
124 GUILayout.Label("The actual authentication must be done by a web-service which you host and customize. Example sourcecode for these services is available on the docs page.");
125
126 GUILayout.Label("For this demo set the Authentication URL in the Dashboard to:\nhttp://photon.webscript.io/auth-demo-equals");
127 GUILayout.Label("That authentication-service has no user-database. It confirms any user if 'name equals password'.");
128
129 GUILayout.Space(10);
130 if (GUILayout.Button("Configure Authentication (Dashboard)"))
131 {
132 Application.OpenURL("https://cloud.exitgames.com/dashboard");
133 }
134 if (GUILayout.Button("Authentication Docs"))
135 {
136 Application.OpenURL("http://doc.exitgames.com/en/pun/current/tutorials/pun-and-facebook-custom-authentication");
137 }
138
139
140 GUILayout.Space(10);
141 if (GUILayout.Button("Back to input"))
142 {
143 SetStateAuthInput();
144 }
145 break;
146
147 case GuiState.AuthInput:
148
149 GUILayout.Label("Authenticate yourself");
150
151 GUILayout.BeginHorizontal();
152 this.authName = GUILayout.TextField(this.authName, GUILayout.Width(Screen.width/4 - 5));
153 GUILayout.FlexibleSpace();
154 this.authToken = GUILayout.TextField(this.authToken, GUILayout.Width(Screen.width/4 - 5));
155 GUILayout.EndHorizontal();
156
157
158 if (GUILayout.Button("Authenticate"))
159 {
160 PhotonNetwork.AuthValues = new AuthenticationValues();
161 PhotonNetwork.AuthValues.SetAuthParameters(this.authName, this.authToken);
162 PhotonNetwork.ConnectUsingSettings("1.0");
163 }
164
165 GUILayout.Space(10);
166
167 if (GUILayout.Button("Help", GUILayout.Width(100)))
168 {
169 SetStateAuthHelp();
170 }
171
172 break;
173 }
174
175 GUILayout.EndArea();
176 }
177 }
for ease of use, this script simply deactivates itself on successful connect
This method is called when Custom Authentication is setup for your app but fails for any reasons.
Unless you setup a custom authentication service for your app (in the Dashboard), this won't be called.
If authentication is successful, this method is not called but OnJoinedLobby, OnConnectedToMaster and the
others will be called.
PhotonNetwork.AuthValues = null; null by default but maybe set in a previous session.
PhotonNetwork.playerName gets set in GUIFriendFinding
a UserID is not used in this case (no AuthValues set)
GUILayout.Label("For this demo set the Authentication URL in the Dashboard to:\nhttp:photon.webscript.ioauth-demo-equals");
GUILayout.Label("For this demo set the Authentication URL in the Dashboard to:\nhttp:photon.webscript.ioauth-demo-equals");
Application.OpenURL("https:cloud.exitgames.comdashboard");
Application.OpenURL("http:doc.exitgames.comenpuncurrenttutorialspun-and-facebook-custom-authentication");